package com.ericpol.patternobject;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class Page {

	protected int Timeout = 30;
	protected WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	public Page(WebDriver driver) {
		this.driver = driver;
	}

	protected WebElement findElement(String field) {			
		return driver.findElement(By.xpath(field));
	}
		
	protected void insertText(String field,String text){
		WebElement element = findElement(field);
		element.clear();
		element.sendKeys(text);		
	}
	
	protected void assertThatElementNotPresent(String field) {
		try{
			driver.findElement(By.xpath(field));
			Assert.fail();
		} catch(NoSuchElementException e){
		}
	}
	
	protected void assertLinkIsOnPage(String text) {
		AssertJUnit.assertEquals(text, driver.findElement(By.linkText(text)).getText());
	}
	
	protected void assertIsLogoutVisible(String field) {
		AssertJUnit.assertEquals("Logout",findElement(field).getText());
	}
	
	protected void chooseAddress(String link) {
		driver.get(link);
	}

	protected void click(By by) {
		driver.findElement(by).click();
	}
	
	protected void clickXPath(String field) {
		driver.findElement(By.xpath(field)).click();
	}

}